package com.ecric.http.webService;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;

import com.ecric.http.R;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;

@EActivity(R.layout.activity_web_service_test)
public class WebServiceTestActivity extends AppCompatActivity {

    @ViewById(R.id.et_ws)
    EditText editText;
    @ViewById(R.id.et_ws2)
    EditText editText2;

    public static final String SERVICE_NS = "";
    public static final String SERVICE_URL = "";

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what){
                case 0x123:
                    editText.setText(msg.obj.toString());
                    break;
                case 0x234:
                    editText2.setText(msg.obj.toString());
                    break;
            }
            return false;
        }
    });
    private HttpTransportSE httpTransportSE;

    @AfterViews
    void initData(){
        //调用方法
        String methodName = "getUserList";
        //创建HttpTransportSE传输对象
        httpTransportSE = new HttpTransportSE(SERVICE_URL);
        httpTransportSE.debug = true;
        //使用SOAP1.1 协议创建Envelop对象 SoapSerializationEnvelope是HttpTransportSE调用WS信息载体;
        //客户端需要向WS传数据，需要把SoapObject对象指派给SoapSerializationEnvelope属性bodyOut
        //同时服务器响应的SOAP信息也许需要通过该对象的bodyIn来获取
        SoapSerializationEnvelope sse = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        //
        SoapObject soapObject = new SoapObject(SERVICE_NS,methodName);
        soapObject.addProperty("arg0","客户端参数");
        sse.bodyOut = soapObject;
    }

    @Background
    void call( SoapSerializationEnvelope sse){
        try {
            httpTransportSE.call(null,sse);
            if (sse.getResponse() != null){
                //获取服务器返回的SOAP信息
                SoapObject soResult = (SoapObject) sse.bodyIn;
                //接下来就是从SoapObject对象中解析响应数据的过程来
                SoapObject detail1 = (SoapObject) soResult.getProperty(0);
                SoapObject detail2 = (SoapObject) soResult.getProperty(1);
                StringBuffer buffer = new StringBuffer();
                buffer.append("用户名：")
                        .append(detail1.getProperty(3))
                        .append("\n密码")
                        .append(detail1.getProperty(0))
                        .append("\n身高")
                        .append(detail1.getProperty(1));
                Message message = handler.obtainMessage();
                message.what = 0x123;
                handler.sendMessage(message);

            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
    }

}
